package com.library.manager.servlet;

import com.alibaba.fastjson.JSONObject;
import com.library.manager.model.Manager;
import com.library.manager.service.ManagerService;
import com.library.manager.service.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("*.manager")
public class ManagerServlet extends HttpServlet {
    private ManagerService managerService = new ManagerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        switch (path) {
            case "/login.manager":
                login(req, resp);
                break;
            case "/register.manager":
                register(req, resp);
                break;
            case "/logout.manager":
                logout(req, resp);
                break;
            default:
                break;
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        
        Manager manager = managerService.login(username, password);
        
        JSONObject result = new JSONObject();
        if (manager != null) {
            // 登录成功，将管理员信息存入session
            HttpSession session = req.getSession();
            session.setAttribute("manager", manager);
            
            result.put("success", true);
            result.put("message", "登录成功");
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(result.toString());
    }

    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String fullname = req.getParameter("fullname");
        
        Manager manager = new Manager();
        manager.setManagerusername(username);
        manager.setManagerpassword(password);
        manager.setManagerfullname(fullname);
        
        boolean success = managerService.register(manager);
        
        JSONObject result = new JSONObject();
        if (success) {
            result.put("success", true);
            result.put("message", "注册成功");
        } else {
            result.put("success", false);
            result.put("message", "注册失败，用户名可能已存在");
        }
        
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(result.toString());
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 清除session
        HttpSession session = req.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        
        // 重定向到登录页面
        resp.sendRedirect(req.getContextPath() + "/login.html");
    }
} 